package org.yecq.tradedata;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.yecq.tradedata.service.BollService;
import org.yecq.tradedata.service.FuturesService;

/**
 *
 * @author yecq
 */
public class Main {

    public static void main(String[] args) {
        if (args == null || args.length == 0) {
            showHelp();
            return;
        }

        if (args.length == 1 && args[0].trim().equalsIgnoreCase("futures-init")) {
            getFuturesService().initFuturesCode();
        } else if (args.length == 1 && args[0].trim().equalsIgnoreCase("futures-data-init")) {
            getFuturesService().getAndSaveHistoryDatasOfCode0();
        } else if (args.length == 1 && args[0].trim().equalsIgnoreCase("futures-data-today")) {
            getFuturesService().getAndSaveTodayOfCode0();
        } else if (args.length == 1 && args[0].trim().equalsIgnoreCase("clear-exception-days")) {
            getFuturesService().clearExceptionDateOfCode0();
        } else if (args.length == 2 && args[0].trim().equalsIgnoreCase("futures-revise")) {
            getFuturesService().updateAllByDate(args[1].trim());
        } else if (args.length == 1 && args[0].trim().equalsIgnoreCase("futures-boll-rebuild-all")) {
            getBollService().reComputeDataFuturesAll();
        } else if (args.length == 2 && args[0].trim().equalsIgnoreCase("futures-boll-rebuild-all-after")) {
            getBollService().reComputeDataFuturesAllAfter(args[1].trim());
        } else if (args.length == 2 && args[0].trim().equalsIgnoreCase("futures-boll-rebuild")) {
            getBollService().reComputeDataFutures(args[1].trim());
        } else if (args.length == 3 && args[0].trim().equalsIgnoreCase("futures-boll-rebuild-after")) {
            getBollService().reComputeDataFuturesAfter(args[1].trim(), args[2].trim());
        } else {
            showHelp();
            System.out.print("参数个数：" + args.length + "  ");
            for (String arg : args) {
                System.out.print(arg + ",");
            }
            System.out.println();
        }
    }

    private static FuturesService getFuturesService() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        return ctx.getBean(FuturesService.class);
    }

    private static BollService getBollService() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        return ctx.getBean(BollService.class);
    }

    private static void showHelp() {
        int index = 1;
        System.out.println("用法" + (index++) + "： " + getScriptName() + " futures-init");
        System.out.println("用法" + (index++) + "： " + getScriptName() + " futures-data-init");
        System.out.println("用法" + (index++) + "： " + getScriptName() + " futures-data-today");
        System.out.println("用法" + (index++) + "： " + getScriptName() + " clear-exception-days");
        System.out.println("用法" + (index++) + "： " + getScriptName() + " futures-revise 日期");
        System.out.println("用法" + (index++) + "： " + getScriptName() + " futures-boll-rebuild-all");
        System.out.println("用法" + (index++) + "： " + getScriptName() + " futures-boll-rebuild-all-after 开始日期");
        System.out.println("用法" + (index++) + "： " + getScriptName() + " futures-boll-rebuild 代号");
        System.out.println("用法" + (index++) + "： " + getScriptName() + " futures-boll-rebuild-after 代号 开始日期");
    }

    // 判断所在操作系统
    private static String getScriptName() {
        String os = System.getProperties().getProperty("os.name").toLowerCase();
        if (os.startsWith("windows")) {
            return "run.cmd";
        } else {
            return "./run.sh";
        }
    }
}
